package com.iogame.iogamevalidatortest;

import com.iohao.game.action.skeleton.protocol.wrapper.IntPb;
import com.iohao.game.common.kit.ProtoKit;
import lombok.extern.slf4j.Slf4j;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@Slf4j
@SpringBootApplication
public class JavaXClientApplication {
    public static void main(String[] args) throws Exception {
        SpringApplication.run(JavaXClientApplication.class, args);

        var wsUrl_javax = "ws://127.0.0.1:10100/websocket";
        var wsUrl_jakarta = "ws://127.0.0.1:20100/websocket";

        new Thread(new Runnable() {
            @Override
            public void run() {
                log.info("测试连接javax server");
                try {
                    login(wsUrl_javax);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        }).start();

        new Thread(new Runnable() {
            @Override
            public void run() {
                log.info("测试连接jakarta server");
                try {
                    login(wsUrl_jakarta);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        }).start();


    }

    private static void login(String wsUrl) throws Exception {
        WebSocketClientConnector webSocketClientConnector = new WebSocketClientConnector(wsUrl);
        webSocketClientConnector.setResponse(new IResponse() {
            @Override
            public void response(int cmd, int subCmd, byte[] data) {
                IntPb intPb = ProtoKit.parseProtoByte(data, IntPb.class);
                log.info("{}登录结果：{}", wsUrl, intPb.intValue);
            }
        });
        webSocketClientConnector.connect();
        UserReq userReq = new UserReq();
        userReq.setUsername("test");
        userReq.setPassword("123456");
        webSocketClientConnector.request(1, 1, userReq);

        Thread.sleep(1000);
        userReq.setUsername("test12345");
        webSocketClientConnector.request(1, 1, userReq);
    }
}
